package ohos.opengl;

import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.FileInputStream;
import java.io.InputStream;

public class ResourceHelper {

    public static InputStream getRawFileByPath(Context context, String path) {
        String filePath = String.format("assets/entry/resources/rawfile/%s", path);
        return context.getClass().getClassLoader().getResourceAsStream(filePath);
    }

    public static InputStream getRawFileByName(ResourceManager resourceManager, String name) {
        try {
            return resourceManager.getRawFileEntry("/resources/rawfile/" + name).openRawFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PixelMap getRawImageByName(ResourceManager resourceManager, String name) {
        try {
            Resource resource = resourceManager.getRawFileEntry("/resources/rawfile/" + name).openRawFile();
            ImageSource imageSource = ImageSource.create(resource, null);
            return imageSource.createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PixelMap getImageByPath(String path) {
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            ImageSource imageSource = ImageSource.create(fileInputStream, null);
            return imageSource.createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PixelMap getImageByByte(byte[] data) {
        try {
            ImageSource imageSource = ImageSource.create(data, null);
            return imageSource.createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Resource getRawResourceById(Context context, int id) {
        try {
            return context.getResourceManager().getResource(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Resource getRawResourceById(ResourceManager resourceManager, int id) {
        try {
            return resourceManager.getResource(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getResourceName(Context context, int id) {
        try {
            return context.getResourceManager().getIdentifier(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PixelMap getPixelMapById(Context context, int id) {
        try {
            Resource resource = context.getResourceManager().getResource(id);
            ImageSource imageSource = ImageSource.create(resource, null);
            return imageSource.createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //todo
    public static String getResourceTypeName(ResourceManager resourceManager, int id) {
        return null;
    }

    //todo
    public static String getResourcePackageName(ResourceManager resourceManager, int id) {
        return null;
    }
}
